﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using AimGameLibrary;
using AimGameEngine.EditorObject;

namespace AimGameEngine.NodeEditorObject
{
    public class NodeRectangle : NodeBaseControl
    {
        public Rectangle myrectangle;

        protected String NameX = "NULL";
        protected String NameY = "NULL";
        protected String NameWidth = "NULL";
        protected String NameHeight = "NULL";

        public NodeRectangle (SpriteBatch inputspritebatch, Camera inputcamera, NodeData inputData
            , List<IDraw> MyListDraw, List<IUpdateMainTime> MyListUpdateTime, Dictionary<String, NodeObject> MyListObject, Dictionary<String, NodeObject> MyListType)
            : base(inputspritebatch, inputcamera, inputData, MyListDraw, MyListUpdateTime, MyListObject, MyListType)
	    {
            myrectangle = new Rectangle(0, 0, 100, 100);

            Connecter outputRectangle = new Connecter(inputspritebatch, inputcamera, Syco.Output, NodeTypeValue.Rectangle, this);
            ListConnectpoint.Add(outputRectangle);

            //Connecter inputRectangle = new Connecter(inputspritebatch, inputcamera, Syco.Input, NodeTypeValue.Rectangle, this);
            //ListConnectpoint.Add(inputRectangle);
	    }

        public override void  SetupInput(Connecter Thisconnect, Connecter connectOutput)
        {
            if (InputMode == NodeInputMode.Rectangle_1)
                MyData.Data = connectOutput.MyNode.MyData.MyName;
            else if (InputMode == NodeInputMode.Int_4)
            {
                        switch (ListConnectpointInput.IndexOf(Thisconnect))
                        {
                            case 0: NameX = connectOutput.MyNode.MyData.MyName; break;
                            case 1: NameY = connectOutput.MyNode.MyData.MyName; break;
                            case 2: NameWidth = connectOutput.MyNode.MyData.MyName; break;
                            case 3: NameHeight = connectOutput.MyNode.MyData.MyName; break;
                        }
                MyData.Data = NameX + "|" + NameY + "|" + NameWidth + "|" + NameHeight;
            }
        }

        public override void ClearInput(Connecter ThisInputConnect,Connecter ThisConnectClear, string NameThisClear)
        {
            //base.ClearInput(ThisConnectClear, NameThisClear);
            MyData.Data = MyData.Data.Replace(NameThisClear, "NULL");
        }

        public override void ShowProprety(ArrayList Temp, CallBack input)
        {
            base.ShowProprety(Temp, input);
            PropretyRectangle propretyprectangle = new PropretyRectangle(this);
            //propretyprectangle.Deactivate += new EventHandler(input.Deactivate);
            propretyprectangle.okButton.Click += new EventHandler(input.Deactivate);
            propretyprectangle.ShowDialog();
            
        }

        public override void SetupProprety()
        {
            base.SetupProprety();

            if (InputMode == NodeInputMode.Rectangle_1)
            {
                Connecter inputRectangle = new Connecter(this.spritebatch, this.camera, Syco.Input, NodeTypeValue.Rectangle, this);
                ListConnectpoint.Add(inputRectangle);
            }
            else if (InputMode == NodeInputMode.Int_4)
            {
                Connecter inputIntX = new Connecter(this.spritebatch, this.camera, Syco.Input, NodeTypeValue.Int, this);
                ListConnectpoint.Add(inputIntX);

                Connecter inputIntY = new Connecter(this.spritebatch, this.camera, Syco.Input, NodeTypeValue.Int, this);
                ListConnectpoint.Add(inputIntY);

                Connecter inputIntWidth = new Connecter(this.spritebatch, this.camera, Syco.Input, NodeTypeValue.Int, this);
                ListConnectpoint.Add(inputIntWidth);

                Connecter inputIntHight = new Connecter(this.spritebatch, this.camera, Syco.Input, NodeTypeValue.Int, this);
                ListConnectpoint.Add(inputIntHight);
            }

            this.AfterSetupProprety();

        }

        protected override Dictionary<string, NodeObject> GetDictionaryNode(Dictionary<string, NodeObject> TempNode)
        {
            if(NameX != null)
            if (this.MyDictionaryObject.ContainsKey(NameX))
                TempNode.Add(NameX, this.MyDictionaryObject[NameX]);

            if(NameY != null)
            if (this.MyDictionaryObject.ContainsKey(NameY))
                TempNode.Add(NameY, this.MyDictionaryObject[NameY]);

            if(NameWidth != null)
            if (this.MyDictionaryObject.ContainsKey(NameWidth))
                TempNode.Add(NameWidth, this.MyDictionaryObject[NameWidth]);

            if(NameHeight != null)
            if (this.MyDictionaryObject.ContainsKey(NameHeight))
                TempNode.Add(NameHeight, this.MyDictionaryObject[NameHeight]);

            return base.GetDictionaryNode(TempNode);
        }

        public override void SaveData(System.Collections.ArrayList Thislist, SaveMode inputsavemode)
        {
            base.SaveData(Thislist,inputsavemode);

            Thislist.Add(InputMode);
            Thislist.Add(myrectangle);
        }

        public override void LoadData(Identification getdata, int IndexData)
        {
            base.LoadData(getdata, IndexData);

            if (getdata.Items.GetValue(IndexData + 1).GetType() == typeof(NodeInputMode))
            InputMode = (NodeInputMode)getdata.Items.GetValue(IndexData + 1);

            if (getdata.Items.GetValue(IndexData + 2).GetType() == typeof(Rectangle))
            myrectangle = (Rectangle)getdata.Items.GetValue(IndexData + 2);

            SetupProprety();
        }
    }
}
